<template>
  <div>
    <div class="box-card">
      <div class="box-header">
        <div class="header-top">
          <div class="header-left">
            <el-input
              v-model="input"
              placeholder="根据用户名搜索"
              style="width: 200px; margin-right: 15px"
            ></el-input>
            <el-row>
              <el-button class="header-btn" @click="clearInput">清空</el-button>
              <el-button type="primary" class="header-btn" @click="searchInput"
                >搜索</el-button
              >
            </el-row>
          </div>
          <el-button
            type="success"
            class="header-right"
            @click="openDialog('add')"
            ><i class="el-icon-edit"></i> 新增权限</el-button
          >
        </div>
        <div class="header-content">
          <i class="el-icon-info"></i>
          <span>共{{ total }}条记录</span>
        </div>
      </div>
      <el-table
        label-width="150px"
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{ background: '#f4f4f5' }"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="title" label="用户名" align="center" width="450">
        </el-table-column>
        <el-table-column
          prop="create_date"
          label="日期"
          align="center"
          width="350"
          sortable
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="edit1"
              @click="openDialog('edit', row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="edit2"
              @click="open(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagesize"
        :total="total"
        layout="prev,pager, next,sizes, jumper"
      >
      </el-pagination>
    </div>

    <!-- 弹出框 -->
    <usersAdd
      :pageTitle="diologTitle"
      :dialogFormVisible.sync="dialogFormVisible"
      ref='userAdd'
      @getInit='initTable'
    ></usersAdd>
  </div>
</template>

<script>
import { list, remove } from '@/api/base/permissions.js'
import usersAdd from '../components/permissions-add.vue'
export default {
  components: {
    usersAdd
  },
  data () {
    return {
      input: '',
      tableData: [],
      page: 1,
      pagesize: 10,
      total: 0,
      showDialog: false,
      dialogFormVisible: false,
      titleType: 'add'
    }
  },

  async created () {
    this.initTable()
  },

  methods: {
    async initTable () {
      const { data } = await list({
        page: this.page,
        pagesize: this.pagesize,
        title: this.input
      })
      console.log(data)
      data.list.forEach(item => {
        item.create_date = item.create_date.substring(0, 10)
      })
      this.tableData = data.list
      this.total = data.counts
    },
    // pagesize改变时
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.initTable()
    },
    // page改变时
    handleCurrentChange (val) {
      this.page = val
      this.initTable()
    },
    // 清空input
    clearInput () {
      this.input = ''
      this.loading = true
      this.initTable()
      this.loading = false
    },
    // 搜索input
    searchInput () {
      this.initTable()
    },
    // 删除取消确认按钮
    open (id) {
      this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发删除请求
          await remove({ id })
          this.initTable()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
    },

    // 点击修改用户按钮
    async openDialog (type, id) {
      this.dialogFormVisible = true
      if (type === 'edit') {
        this.$refs.userAdd.hanldeEditForm(id)
      }
    }
  },
  computed: {
    diologTitle () {
      if (this.titleType === 'add') {
        return '创建权限组'
      } else if (this.titleType === 'edit') {
        return '编辑权限组'
      } else {
        return '创建权限组'
      }
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  margin: 20px;
  background-color: #fff;
  padding: 20px;
  .box-header {
    .header-top {
      height: 45px;
      margin-bottom: 10px;
      .header-left {
        display: flex;
        .header-btn {
          width: 60px;
          font-size: 12px;
        }
      }
      .header-right {
        width: 105px;
        height: 32px;
        font-size: 12px;
        float: right;
        margin-top: -36px;
        .el-icon-edit {
          margin-left: -6px;
        }
      }
    }
    .header-content {
      background-color: #f4f4f5;
      color: #909399;
      font-size: 13px;
      padding-left: 8px;
      height: 40px;
      line-height: 40px;
      margin-bottom: 20px;
      .el-icon-info {
        margin-right: 5px;
      }
    }
  }
  .edit1 {
    color: #409eff;
    background: #ecf5ff;
    border-color: #b3d8ff;
    &:hover {
      background-color: #409eff;
      color: #fff;
    }
  }
  .edit2 {
    color: #f56c6c;
    background: #fef0f0;
    border-color: #fbc4c4;
    &:hover {
      background-color: #f56c6c;
      color: #fff;
    }
  }
  .el-pagination {
    height: 40px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-top: 20px;
  }
}
.el-icon-warning {
  color: #e6a23c;
  font-size: 25px;
  margin-right: 10px;
}
.el-dialog {
  .el-form {
    width: 400px;
    margin: auto;
    .el-input {
      width: 310px;
      height: 35px;
    }
  }
  .dialog-footer {
    margin-top: 60px;
    text-align: center;
  }
}
</style>
